﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class Menu_E : PageBaseAdmin
    {
        private readonly D_T_Menu_Exp _Dal = new D_T_Menu_Exp();

        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserToolBar1.EditInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              (i, but) =>
              {
                  switch (i)
                  {
                      case 1:
                          but.CausesValidation = false;
                          break;
                      default:
                          break;
                  }

              },
             UserConst.Exp_ToolBarErr
           );

            if (!IsPostBack)
            {
                var LD = _Dal.GetMenuList();
                var id = Request.Params["ID"].Exp_IntTryParse();
                this.DropDownList1.DataSource = LD;
                this.DropDownList1.DataTextField = "MenuName";
                this.DropDownList1.DataValueField = "id";
                this.DropDownList1.DataBind();
                ViewState.Add("ID", id);


                if (id > 0)
                {
                    var model = _Dal.GetModel(id);
                    if (model == null || !model.Flag)
                    {
                        //请注意,只要跳转回显示页面在,则必须传入Skey
                        var sKey = this.WebUserToolBar1.EditSKey;
                        this.Exp_PageAlterHref("找不到相关资料", "menu.aspx?" + sKey);
                    }
                    else
                    {
                        this.DropDownList1.SelectedValue = model.MenuParentID.Exp_Trim();
                        this.TextBox2.Text = model.MenuName;
                        this.TextBox3.Text = model.MenuURL;
                    }
                }
            }
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var model = new M_T_MenuExp();

            model.MenuParentID = this.DropDownList1.SelectedValue.Exp_IntTryParse();
            if (model.MenuParentID <= 0)
            {
                model.MenuLevel = 1;
                model.MenuParentID = 0;
            }
            else
            {
                var PMode = _Dal.GetModel(model.MenuParentID);
                if (PMode == null || PMode.Flag == false)
                {
                    this.Exp_PageAlter("父菜单不存在");
                    return;
                }
                model.MenuLevel = PMode.MenuLevel + 1;
                model.MenuParentID = PMode.id;
            }
            model.Flag = true;
            model.id = ViewState["ID"].Exp_IntTryParse();
            model.MenuName = TextBox2.Text.Exp_Trim();
            model.MenuParentList = "";
            model.MenuSonList = "";
            model.MenuURL = TextBox3.Text.Exp_Trim();


            /*
                公共的差错校验
             */

            if (_Dal.IsMenuURL(model.MenuURL, model.id))
            {
                this.Exp_PageAlter("当前菜单地址已经存在");
                return;
            }

            var iRef = 0;
            var id = 0;
            var le = new Log_Type();
            if (0 >= model.id)
            {
                iRef = _Dal.Add(model);
                le = Log_Type.add;
                id = iRef;
            }
            else
            {
                /*
                    修改的专用判断
                 */
                if (model.id == model.MenuParentID)
                {
                    this.Exp_PageAlter("您当前修改的资料和上级菜单相同");
                    return;
                }

                var Temp = _Dal.GetModel(model.id);
                if (Temp == null || !Temp.Flag)
                {
                    this.Exp_PageAlter("当前菜单资料不存在,可能已经被其他人删除");
                    return;
                }
                if (Temp.MenuParentID != model.MenuParentID)
                {
                    if (_Dal.GetSonCount(model.id) > 0)
                    {
                        this.Exp_PageAlter("当前菜单还有子菜单,不能进行上级菜单的修改操作");
                        return;
                    }
                }

                iRef = _Dal.Update(model);
                le = Log_Type.update;
                id = model.id;
            }

            if (iRef > 0)
            {
                this.Exp_PageWriteLog(le, "菜单信息", id);
                var sKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("操作成功", "menu.aspx?" + sKey);

            }
            else
            {
                this.Exp_PageAlter("操作失败");
            }

        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("menu.aspx?" + sKey);
        }
    }
}
